<?php

Class PowerModel
{
    public function __construct()
    {
        $this->db = Yaf_Registry::get('_db');
    }

    public function find($id)
    {
        $sql = "select * from t_power where id = $id";
        $result = $this->db->get_row($sql);
        if ($result){
            return $result;
        }else {
            return NULL;
        }
    }
    
    public function ufind($controller,$aciton){
    	$sql = "select * from t_power where controller = '$controller' and action = '$aciton'";
    	$result = $this->db->get_row($sql);
    	if ($result){
    		return $result;
    	}else {
    		return NULL;
    	}
    }
	
    public function selectAll($start = 0, $length = 20, $searchkey = '')
    {
        $wheresql = ' where 1=1 ';
        if (isset($searchkey) && !empty($searchkey)){
            $wheresql .= " and a.title like '%$searchkey%'";
        }
        
        // 获取总数
        $countsql = "select count(a.id) from t_power as a " . $wheresql;
        $total = $this->db->get_var($countsql);
        
        // 获取分页数据
        $sql = "select a.id, a.title, concat(a.controller, '/', a.action) as contr_action, b.title as parent_title, a.type, a.status 
                    from t_power as a
                    left join t_power as b on a.parentid = b.id
                    " . $wheresql . " order by a.id limit $start, $length";

        $result = $this->db->get_results($sql);

        return array($total, $result);
    }
    
    public function getallpower()
    {
        $sql = "select id, title, parentid, concat(controller, '/', action) as contr_action
                    from t_power
                    where status = 1";
        $powers = $this->db->get_results($sql);
        return $powers;
    }

    public function insert($info)
    {
        $priority = $info['priority'];
        $title = $info['title'];
        $controller = $info['controller'];
        $action = $info['action'];
        $parentid = $info['parentid'];
        $type = $info['type'];
        $createtime = date('Y-m-d H:i:s');
        
        $sql = "insert into t_power(title, controller, action, parentid, type, priority, createtime, status)
                    values('$title', '$controller', '$action', $parentid, $type, $priority, '$createtime', 1)";
                    
        $result = $this->db->query($sql);
        return $result;
    }

    public function update($info)
    {
        $priority = $info['priority'];
        $title = $info['title'];
        $controller = $info['controller'];
        $action = $info['action'];
        $parentid = $info['parentid'];
        $type = $info['type'];
        $status = $info['status'];
        $id = $info['id'];
        
        $sql = "update t_power 
                    set priority = $priority, 
                        title = '$title',
                        controller = '$controller',
                        action = '$action',
                        parentid = $parentid,
                        type = $type,
                        status = $status 
                    where id = $id";
        $result = $this->db->query($sql);
        return $result;
    }
}
